import csv
from decimal import Decimal
from datetime import date
from typing import Dict
from decimal import Decimal

class WeekReportExtInfo:
    def __init__(self):
        # 港元人民币汇率
        self.hkd_to_cny_rate = Decimal(0)
        # 富途年初资产
        self.futu_initial_assets = Decimal(0)
        # 富途本周净流入
        self.futu_weekly_net_inflow = Decimal(0)
        # 东方财富普通账户本周净流入
        self.em_normal_weekly_net_inflow = Decimal(0)
        # 东方财富普通账户本周收益
        self.em_normal_weekly_profit = Decimal(0)
        # 东方财富普通账户今年收益
        self.em_normal_yearly_profit = Decimal(0)
        # 东方财富普通账户至今收益
        self.em_normal_total_profit = Decimal(0)
        # 东方财富信用账户本周净流入
        self.em_credit_weekly_net_inflow = Decimal(0)
        # 东方财富信用账户本周收益
        self.em_credit_weekly_profit = Decimal(0)
        # 东方财富信用账户今年收益
        self.em_credit_yearly_profit = Decimal(0)
        # 东方财富信用账户至今收益
        self.em_credit_total_profit = Decimal(0)
        # 东方财富期权账户总资产
        self.em_option_total_assets = Decimal(0)
        # 东方财富期权账户保证金
        self.em_option_margin_balance = Decimal(0)
        # 东方财富期权账户本周净流入
        self.em_option_weekly_net_inflow = Decimal(0)
        # 东方财富期权账户本周收益
        self.em_option_weekly_profit = Decimal(0)

        # 支付宝基金本周资产
        self.alipay_fund_assets = Decimal(0)
        # 支付宝基金累计收益
        self.alipay_fund_total_profit = Decimal(0)
        # 招行基金本周资产
        self.cmb_fund_assets = Decimal(0)
        # 招行基金累计收益
        self.cmb_fund_total_profit = Decimal(0)
        # 天天基金本周资产
        self.tiantian_fund_assets = Decimal(0)
        # 天天基金累计收益
        self.tiantian_fund_total_profit = Decimal(0)
        # 且慢本周资产
        self.qieman_fund_assets = Decimal(0)
        # 且慢累计收益
        self.qieman_fund_total_profit = Decimal(0)
        # 负债_银行
        self.bank_debts = Decimal(0)
        # 负债_姐
        self.sister_debts = Decimal(0)
        # 负债_哥
        self.brother_debts = Decimal(0)
        # 负债_爸
        self.father_debts = Decimal(0)
        # 十字星本周收益率
        self.szx_rate = Decimal(0)
        # 报告日期
        self.report_date = date.today()


class WeekReportExtInfoParser:
    def __init__(self):
        pass

    def parse(self, file_path: str) -> WeekReportExtInfo:
        """
        从CSV文件解析周度报告扩展信息
        :param file_path: CSV文件路径
        :return: 周度报告扩展信息对象
        """
        info = WeekReportExtInfo()
        print(f"load file {file_path}")
        try:
            with open(file_path, 'r', encoding='GBK') as file:
                reader = csv.reader(file)
                for line in reader:
                    if len(line) < 2:
                        continue
                    field_name = line[0].strip()
                    value_str = line[1].strip()
                    value_str = self.process_number_with_commas(value_str)
                    if value_str:
                        self.set_field_value(info, field_name, value_str)

            if info.report_date is None:
                info.report_date = date.today()

        except Exception as e:
            print(f"文件读取异常: {e}")

        return info

    def process_number_with_commas(self, value: str) -> str:
        """
        处理带引号和逗号的数字格式
        例如："1,000,000" -> 1000000
        :param value: 原始字符串值
        :return: 处理后的数值字符串
        """
        if not value:
            return value
        return value.replace('"', '').replace(',', '').strip()

    def set_field_value(self, info: WeekReportExtInfo, field_name: str, value_str: str):
        """
        根据字段名设置WeekReportExtInfo对象的对应值
        :param info: 周度报告扩展信息对象
        :param field_name: 字段名
        :param value_str: 字符串值
        """
        try:
            value = Decimal(value_str)
            if field_name == "港元人民币汇率":
                info.hkd_to_cny_rate = value
            elif field_name == "富途年初资产":
                info.futu_initial_assets = value
            elif field_name == "富途本周净流入":
                info.futu_weekly_net_inflow = value
            elif field_name == "东方财富普通账户本周净流入":
                info.em_normal_weekly_net_inflow = value
            elif field_name == "东方财富普通账户本周收益":
                info.em_normal_weekly_profit = value
            elif field_name == "东方财富普通账户今年收益":
                info.em_normal_yearly_profit = value
            elif field_name == "东方财富普通账户至今收益":
                info.em_normal_total_profit = value
            elif field_name == "东方财富信用账户本周净流入":
                info.em_credit_weekly_net_inflow = value
            elif field_name == "东方财富信用账户本周收益":
                info.em_credit_weekly_profit = value
            elif field_name == "东方财富信用账户今年收益":
                info.em_credit_yearly_profit = value
            elif field_name == "东方财富信用账户至今收益":
                info.em_credit_total_profit = value

            #     # 东方财富期权账户总资产
            # self.em_option_total_assets = Decimal(0)
            # # 东方财富期权账户保证金
            # self.em_option_margin_balance = Decimal(0)
            elif field_name == "东方财富期权账户总资产":
                info.em_option_total_assets = value
            elif field_name == "东方财富期权账户保证金":
                info.em_option_margin_balance = value
            elif field_name == "东方财富期权账户本周净流入":
                info.em_option_weekly_net_inflow = value

            elif field_name == "东方财富期权账户本周收益":
                info.em_option_weekly_profit = value
            elif field_name == "支付宝基金本周资产":
                info.alipay_fund_assets = value
            elif field_name == "支付宝基金累计收益":
                info.alipay_fund_total_profit = value
            elif field_name == "招行基金本周资产":
                info.cmb_fund_assets = value
            elif field_name == "招行基金累计收益":
                info.cmb_fund_total_profit = value
            elif field_name == "天天基金本周资产":
                info.tiantian_fund_assets = value
            elif field_name == "天天基金累计收益":
                info.tiantian_fund_total_profit = value
            elif field_name == "且慢本周资产":
                info.qieman_fund_assets = value
            elif field_name == "且慢累计收益":
                info.qieman_fund_total_profit = value
            elif field_name == "负债_银行":
                info.bank_debts = value
            elif field_name == "负债_姐":
                info.sister_debts = value
            elif field_name == "负债_哥":
                info.brother_debts = value
            elif field_name == "负债_爸":
                info.father_debts = value
            elif field_name == "十字星":
                info.szx_rate = value

            elif field_name == "报告日期":
                try:
                    info.report_date = date.fromisoformat(value_str)
                except ValueError:
                    info.report_date = date.today()
        except ValueError:
            print(f"无法解析字段 '{field_name}' 的值: {value_str}")
# 使用示例
if __name__ == "__main__":
    try:
        sample_csv = "../../test/data/report/week_report_ext_20250607.csv"
        parser = WeekReportExtInfoParser()
        # 解析CSV文件
        info = parser.parse(sample_csv)

        # 打印解析结果
        print(f"解析成功！{info}")

    except Exception as e:
        print(f"解析出错: {str(e)}")